package com.marketapp.controller;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.marketapp.helper.ServiceException;
import com.marketapp.ws.IEquityDataWS;

@Controller
@RequestMapping(value = { "/home", "/" })
public class HomeController {

	@Autowired
	private IEquityDataWS equityDataWS;

	@Autowired
	private Properties symbolProps;

	@RequestMapping(method = RequestMethod.GET)
	public String getHomeView(Model model) {
		Date today = GregorianCalendar.getInstance().getTime();
		Map<String, String> quoteMapBSE = null;
		Map<String, String> quoteMapNSE = null;
		try {
			quoteMapBSE = equityDataWS.getQuoteDetailsByName(symbolProps
					.getProperty("BSESENSEX"));
			quoteMapNSE = equityDataWS.getQuoteDetailsByName(symbolProps
					.getProperty("NSESENSEX"));
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		model.addAttribute("today", today);
		model.addAttribute("quoteMapBSE", quoteMapBSE);
		model.addAttribute("quoteMapNSE", quoteMapNSE);
		return "market-home";
	}

	@RequestMapping(value = "/getBSEQuote", method = RequestMethod.GET, headers = "Accept=*/*")
	public @ResponseBody
	Map<String,String> getBSEQuote() throws ServiceException {
		Map<String,String>  quoteMapBSE = equityDataWS.getQuoteDetailsByName(symbolProps
				.getProperty("BSESENSEX"));
		System.out.println("Hello");
		return quoteMapBSE;
	}

	@RequestMapping(value = "/getNSEQuote", method = RequestMethod.GET, headers = "Accept=*/*")
	public @ResponseBody
	Map<String,String> getNSEQuote() throws ServiceException {
		Map<String,String>  quoteMapNSE = equityDataWS.getQuoteDetailsByName(symbolProps
				.getProperty("NSESENSEX"));
		return quoteMapNSE;
	}

	public IEquityDataWS getEquityDataWS() {
		return equityDataWS;
	}

	public void setEquityDataWS(IEquityDataWS equityDataWS) {
		this.equityDataWS = equityDataWS;
	}

	public Properties getSymbolProps() {
		return symbolProps;
	}

	public void setSymbolProps(Properties symbolProps) {
		this.symbolProps = symbolProps;
	}
}
